package com.mall.controller;

import com.alibaba.fastjson.JSON;
import com.mall.bean.SystemPermission;
import com.mall.bean.SystemRole;
import com.mall.bean.User;
import com.mall.service.SystemRolePermissionService;
import com.mall.service.SystemRoleService;
import com.mall.service.SystemUserRoleService;
import com.mall.vo.Response;
import com.mall.vo.RoleVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @PageName com.mall.controller
 * @Description 角色管理控制层
 * @Author 何全裕
 * @Create 2017-09-03 20:10
 * @Version 1.0
 **/
@Controller
@RequestMapping("/role")
public class SystemRoleController {

    @Autowired
    private SystemRoleService SystemRoleService;

    @Autowired
    private SystemRolePermissionService systemRolePermissionServiceImpl;

    @Autowired
    private SystemUserRoleService systemUserRoleService;

    @ResponseBody
    @RequestMapping("/getRoleListForPage")
    public Map<String,Object> getRoleListForPage() {
        Map<String,Object> map = new HashMap<String, Object>();
        List<SystemRole> systemRoleList = SystemRoleService.getSystemRoleList();
        List<RoleVo> roleVoList = new ArrayList<RoleVo>();
        for (SystemRole role : systemRoleList){
            RoleVo roleVo = new RoleVo();
            try {
                BeanUtils.copyProperties(roleVo, role);
            } catch (Exception e) {
                e.printStackTrace();
            }
            roleVo.setAvailableVo(role.getAvailable().equals("1")?"启用":"停用");
            roleVoList.add(roleVo);
        }
        map.put("code",0);
        map.put("msg","");
        map.put("count",roleVoList.size());
        map.put("data",roleVoList);
        return map;
    }

    @RequestMapping("/getRoleList")
    public ModelAndView getRoleList() {
        ModelAndView modelAndView = new ModelAndView("/system_role/SystemRole");
        return modelAndView;
    }

    @RequestMapping("/toRoleAdd")
    public ModelAndView toRoleAdd() {
        ModelAndView modelAndView = new ModelAndView("system_role/SystemRoleAdd");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/insertRole")
    public Response insertRole(SystemRole role){
        role.setId(UUID.randomUUID().toString());
        role.setAvailable("1");
        SystemRoleService.insertRole(role);
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/deleteAllRole")
    public Response deleteAllRole(@RequestParam(value = "ids[]")String[] ids){
        SystemRoleService.deleteAllRole(ids);
        for(String rid : ids){
            systemRolePermissionServiceImpl.deleteRolePermissionByRoleId(rid);
            systemUserRoleService.deleteUserRoleByRoleId(rid);
        }
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/deleteRole")
    public Response deleteAllRole(String id){
        SystemRoleService.deleteRole(id);
        systemRolePermissionServiceImpl.deleteRolePermissionByRoleId(id);
        systemUserRoleService.deleteUserRoleByRoleId(id);
        return new Response("0","","");
    }

    @RequestMapping("/toRoleUpdate")
    public ModelAndView toRoleUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView("system_role/SystemRoleUpdate");
        SystemRole vo = SystemRoleService.getSystemRoleById(id);
        modelAndView.addObject("vo",vo);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/updateRole")
    public Response updateRole(SystemRole role){
        SystemRoleService.updateSystemRole(role);
        return new Response("0","","");
    }

}
